<?php
include ('header.php');
if ($admin==1) {


	/*$res=mysql_query ("SELECT * FROM att_value WHERE value like '%5%'");
while ($v= mysql_fetch_array($res)) {
echo '!!'.$v['id_att'].'<br />';}*/
// Колонка Фамилия Имя препода, Недели предмета, (all-все,chet-четные, nchet-нечетные, число-номера недели), Недели !кроме!, сп, сп_нум в одну. Тип пары (лекция, лаба). Поле  предмет увеличить. По умолчанию отображается расписание на текущий день, если время больше 19.10 то на следующий день.`weekday`  int(1)



mysql_query ("CREATE TABLE IF NOT EXISTS rasp (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`sp` VARCHAR( 3 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`sp_num` INT( 3 ) NOT NULL ,
`predmet` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`predmet_num` int(1) NOT NULL default '1',
`aud` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`week` int(1) NOT NULL default '2',
`weekday` VARCHAR( 9 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;") or die(mysql_error());

	mysql_query ("CREATE TABLE IF NOT EXISTS par_time (
`num` int(1) NOT NULL default '1' PRIMARY KEY ,
`start` time ,
`end` time
) ENGINE = MYISAM") or die(mysql_error());

	$predmet1_1=$_POST['predmet1-1'];
	$predmet0_1=$_POST['predmet0-1'];
	$predmet1_2=$_POST['predmet1-2'];
	$predmet0_2=$_POST['predmet0-2'];
	$predmet1_3=$_POST['predmet1-3'];
	$predmet0_3=$_POST['predmet0-3'];
	$predmet1_4=$_POST['predmet1-4'];
	$predmet0_4=$_POST['predmet0-4'];
	$predmet1_5=$_POST['predmet1-5'];
	$predmet0_5=$_POST['predmet0-5'];
	$predmet1_6=$_POST['predmet1-6'];
	$predmet0_6=$_POST['predmet0-6'];
	
	$predmet1_1_aud=$_POST['predmet1-1-aud'];
	$predmet0_1_aud=$_POST['predmet0-1-aud'];
	$predmet1_2_aud=$_POST['predmet1-2-aud'];
	$predmet0_2_aud=$_POST['predmet0-2-aud'];
	$predmet1_3_aud=$_POST['predmet1-3-aud'];
	$predmet0_3_aud=$_POST['predmet0-3-aud'];
	$predmet1_4_aud=$_POST['predmet1-4-aud'];
	$predmet0_4_aud=$_POST['predmet0-4-aud'];
	$predmet1_5_aud=$_POST['predmet1-5-aud'];
	$predmet0_5_aud=$_POST['predmet0-5-aud'];
	$predmet1_6_aud=$_POST['predmet1-6-aud'];
	$predmet0_6_aud=$_POST['predmet0-6-aud'];
	
	
	$allweek1_1=$_POST['allweek-1-1'];
	$allweek0_1=$_POST['allweek-0-1'];
	$allweek1_2=$_POST['allweek-1-2'];
	$allweek0_2=$_POST['allweek-0-2'];
	$allweek1_3=$_POST['allweek-1-3'];
	$allweek0_3=$_POST['allweek-0-3'];
	$allweek1_4=$_POST['allweek-1-4'];
	$allweek0_4=$_POST['allweek-0-4'];
	$allweek1_5=$_POST['allweek-1-5'];
	$allweek0_5=$_POST['allweek-0-5'];
	$allweek1_6=$_POST['allweek-1-6'];
	$allweek0_6=$_POST['allweek-0-6'];
	
	
if(isset($_POST['sp'])) {$spp=$_POST['sp'];};
if(isset($_POST['sp_num'])) {if(!preg_match('/[^0-9]/ui',$_POST['sp_num'])) {$sp_nump=$_POST['sp_num'];} else { echo 'Хакер?=))';}} else {$sp_nump='';};
if(isset($_POST['weekday'])) {if(!preg_match('/[^0-9]/ui',$_POST['weekday'])) {$weekdayp=$_POST['weekday'];} else { echo 'Хакер?=))';}} else {$weekdayp='';};

if(isset($_GET['sp'])) {$sp=mysql_real_escape_string($_GET['sp']);};
if(isset($_GET['sp_num'])) {if(!preg_match('/[^0-9]/ui',$_GET['sp_num'])) {$sp_num=$_GET['sp_num'];} else { echo 'Хакер?=))';}} else {$sp_num='';};
if(isset($_GET['weekday'])) {if(!preg_match('/[^0-9]/ui',$_GET['weekday'])) {$weekday=$_GET['weekday'];} else { echo 'Хакер?=))';}} else {$weekday='';};
	if ($weekday==1 || $weekdayp==1) {$dayofweek="<u>Понедельник</u>";}
	if ($weekday==2 || $weekdayp==2) {$dayofweek="<u>Вторник</u>";}
	if ($weekday==3 || $weekdayp==3) {$dayofweek="<u>Среда</u>";}
	if ($weekday==4 || $weekdayp==4) {$dayofweek="<u>Четверг</u>";}
	if ($weekday==5 || $weekdayp==5) {$dayofweek="<u>Пятница</u>";}
	if ($weekday==6 || $weekdayp==6) {$dayofweek="<u>Суббота</u>";}
	if ($weekday==7 || $weekdayp==7) {$dayofweek="<u>Воскресенье</u>";}
	if ($weekday==1 || $weekdayp==1) {$dayofweeks="<u>понедельник</u>";}
	if ($weekday==2 || $weekdayp==2) {$dayofweeks="<u>вторник</u>";}
	if ($weekday==3 || $weekdayp==3) {$dayofweeks="<u>среду</u>";}
	if ($weekday==4 || $weekdayp==4) {$dayofweeks="<u>четверг</u>";}
	if ($weekday==5 || $weekdayp==5) {$dayofweeks="<u>пятницу</u>";}
	if ($weekday==6 || $weekdayp==6) {$dayofweeks="<u>субботу</u>";}
	if ($weekday==7 || $weekdayp==7) {$dayofweeks="<u>воскресенье</u>";}

if ($actp=='update') {
	echo '<h1>Обновили расписание на '.$dayofweeks.' для группы '.$spp.'-'.$sp_nump.'</h1>
	<br /><a href="rasp.php?sp='.$spp.'&amp;sp_num='.$sp_nump.'">Продолжить редактирование расписания группы '.$spp.'-'.$sp_nump.'.</a><br />
	<a href="rasp.php?act=edit&amp;sp='.$spp.'&amp;sp_num='.$sp_nump.'&amp;weekday='.$weekdayp.'">Продолжить редактирование расписания группы '.$spp.'-'.$sp_nump.' на тот же день.</a><br />';
	
	
	//заносим данные в цикле.
	for ($np=1;$np<=6;$np++) {
		//четные недели
		$wek=1;
		$nwek=0;		
		if (!empty(${'predmet'.$wek.'_'.$np}) && !empty(${'predmet'.$wek.'_'.$np.'_aud'})) {
			
		$predmetm=${'predmet'.$wek.'_'.$np};
		$audm=${'predmet'.$wek.'_'.$np.'_aud'};
		$zestall = mysql_query("SELECT COUNT(*) FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='2' AND predmet_num='$np'");
list($estall) = mysql_fetch_row($zestall);
if ($estall!=0 && ${'predmet1_'.$np}!=${'predmet0_'.$np}) 
	{
	mysql_query("UPDATE `rasp` SET `week` =  '$nwek' WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='2' AND predmet_num='$np'") or die(mysql_error());
	echo $np.'-пара. Запись перемещена- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на не четной неделе. <br />';
	mysql_query("INSERT INTO `rasp` VALUES (0, '$spp', '$sp_nump', '$predmetm', '$np', '$audm', '$wek', '$weekdayp')") or die(mysql_error());
	echo $np.'-пара. Добавлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на четной неделе. <br />';
	}
	
else {
	
	if ($estall!=0 && ${'predmet1_'.$np}==${'predmet0_'.$np} && ${'predmet_'.$np.'_aud'}==${'predmet_'.$np.'_aud'}) 
	{
	echo $np.'-пара. Обновлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на всех неделях. <br />';
	$messupdallweek=1;
	}
	
else {
	
	$zest = mysql_query("SELECT COUNT(*) FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='$wek' AND predmet_num='$np'");
list($est) = mysql_fetch_row($zest);

		if ($est==0) {
			
		echo $np.'-пара. Добавлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на четной неделе. <br />';
		mysql_query("INSERT INTO `rasp` VALUES (0, '$spp', '$sp_nump', '$predmetm', '$np', '$audm', '$wek', '$weekdayp')") or die(mysql_error());
		} else {
			echo $np.'-пара. Обновлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на четной неделе. <br />';
			$zprid = mysql_query("SELECT id FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='$wek' AND predmet_num='$np'");
			$bprid = mysql_fetch_array($zprid);
			$prid=$bprid['id'];
			mysql_query("UPDATE `rasp` SET `predmet` =  '$predmetm',`aud` = '$audm' WHERE `id` ='$prid'") or die(mysql_error());
			}}}}
			else
		     {$zdest = mysql_query("SELECT id FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='2'");
			$dest = mysql_fetch_array($zdest);
			$destid=$dest['id'];
			if (isset($destid)) {
			mysql_query("UPDATE `rasp` SET `week` =  '$nwek' WHERE `id` ='$destid'");}
				 mysql_query("DELETE FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$wek'") or die(mysql_error());}
			
		//нечетные недели	
		$wek=0;
		$nwek=1;
		if (!empty(${'predmet'.$wek.'_'.$np}) && !empty(${'predmet'.$wek.'_'.$np.'_aud'})) {
		$predmetm=${'predmet'.$wek.'_'.$np};
		$audm=${'predmet'.$wek.'_'.$np.'_aud'};
		
		$zestall = mysql_query("SELECT COUNT(*) FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='2' AND predmet_num='$np'");
list($estall) = mysql_fetch_row($zestall);
if ($estall!=0 && ${'predmet1_'.$np}!=${'predmet0_'.$np})
	{
	mysql_query("UPDATE `rasp` SET `week` =  '$nwek' WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='2' AND predmet_num='$np'") or die(mysql_error());
	echo $np.'-пара. Запись перемещена- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на четной неделе. <br />';
	mysql_query("INSERT INTO `rasp` VALUES (0, '$spp', '$sp_nump', '$predmetm', '$np', '$audm', '$wek', '$weekdayp')") or die(mysql_error());
	echo $np.'-пара. Добавлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на не четной неделе. <br />';
	}
else {
	
	if ($estall!=0 && ${'predmet1_'.$np}==${'predmet0_'.$np} && ${'predmet_'.$np.'_aud'}==${'predmet_'.$np.'_aud'}) 
	{
		if ($messupdallweek!=1) {
	echo $np.'-пара. Обновлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на всех неделях. <br />';
	}}
	
else {
	
	$zest = mysql_query("SELECT COUNT(*) FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='$wek' AND predmet_num='$np'");
list($est) = mysql_fetch_row($zest);
		if ($est==0) {
		echo $np.'-пара. Добавлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на не четной неделе. <br />';
		mysql_query("INSERT INTO `rasp` VALUES (0, '$spp', '$sp_nump', '$predmetm', '$np', '$audm', '$wek', '$weekdayp')") or die(mysql_error());
		} else {
			echo $np.'-пара. Обновлена запись- предмет "'.$predmetm.'" в аудитории '.$audm.' в '.$dayofweeks.' на не четной неделе. <br />';
			$zprid = mysql_query("SELECT id FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='$wek' AND predmet_num='$np'");
			$bprid = mysql_fetch_array($zprid);
			$prid=$bprid['id'];
			mysql_query("UPDATE `rasp` SET `predmet` =  '$predmetm',`aud` = '$audm' WHERE `id` ='$prid'") or die(mysql_error());
			}}}}
			else
		     {$zdest = mysql_query("SELECT id FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='2'");
			$dest = mysql_fetch_array($zdest);
			$destid=$dest['id'];
			if (isset($destid)) {
			mysql_query("UPDATE `rasp` SET `week` =  '$nwek' WHERE `id` ='$destid'");};
				 mysql_query("DELETE FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$wek'") or die(mysql_error());
		}};
	
	
		// если стоиг галка "на всех неделях "
		
		
		for ($np=1;$np<=6;$np++) {
		//четные недели
		$wek=1;
		$nwek=0;
		
		if (isset(${'allweek'.$wek.'_'.$np})) {
		$lessres = mysql_query("SELECT id, predmet FROM rasp WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$wek'") or die(mysql_error());
		$less = mysql_fetch_array($lessres);
		if (!empty($less['id'])) {
			echo $numpar.' пара "'.$less['predmet'].'" с четной недели скопированна на все недели<br/>'; 
			$upid=$less['id']; 
			mysql_query("UPDATE `rasp` SET `week`='2' WHERE `id`='$upid'") or die(mysql_error());
			mysql_query("DELETE FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$nwek'") or die(mysql_error());
			}}
		
		$wek=0;
		$nwek=1;
		
		if (isset(${'allweek'.$wek.'_'.$np})) {
		$lessres = mysql_query("SELECT id, predmet FROM rasp WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$wek'") or die(mysql_error());
		$less = mysql_fetch_array($lessres);
		if (!empty($less['id'])) {
			echo $numpar.' пара "'.$less['predmet'].'" с  не четной недели скопированна на все недели<br/>'; 
			$upid=$less['id']; 
			mysql_query("UPDATE `rasp` SET `week`='2' WHERE `id`='$upid'") or die(mysql_error());
			mysql_query("DELETE FROM `rasp` WHERE
		sp='$spp' AND sp_num='$sp_nump'  AND weekday='$weekdayp' AND predmet_num='$np' AND week='$nwek'") or die(mysql_error());
		}}	
		};
		
		for ($np=1;$np<=6;$np++) {
		if (${'predmet1_'.$np}==${'predmet0_'.$np} && ${'predmet_'.$np.'_aud'}==${'predmet_'.$np.'_aud'}) {
			mysql_query("UPDATE `rasp` SET `week`='2' WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='1' AND predmet_num='$np'") or die(mysql_error());
			mysql_query("DELETE FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='1' AND predmet_num='$np'") or die(mysql_error());
			mysql_query("DELETE FROM `rasp` WHERE sp='$spp' AND sp_num='$sp_nump' AND weekday='$weekdayp' AND week='0' AND predmet_num='$np'") or die(mysql_error());
			}
		}
		
		
		
	echo '<br />';};
	

if ($act=='delete' && $confirm!=1 && !empty($sp) && !empty($sp_num)) {echo '<center><H1>Уверены?<br /> <a href="?act=delete&sp='.$sp.'&sp_num='.$sp_num.'&confirm=1">ДА</a> &nbsp;&nbsp;&nbsp;&nbsp; <a href="?sp='.$sp.'&sp_num='.$sp_num.'">НЕТ</a></h1></center><br /><br /><br />';};
if ($act=='delete' && $confirm==1 && !empty($sp) && !empty($sp_num)) { 
mysql_query("DELETE FROM `rasp` WHERE `sp` = '$sp' AND sp_num = '$sp_num'") or die(mysql_error());
echo '<h1>Расписание для группы '.$sp.'-'.$sp_num.' полностью удалено из базы.</h1>';
unset($sp);
unset($sp_num);
};

if ($act=='edit' && !empty($sp) && !empty($sp_num) && !empty($weekday)) {
	?>
    <form action="rasp.php" method="post" name="rasp">
<fieldset>
<legend>Изменение расписания на <?php echo $dayofweeks; ?> для группы <?php echo $sp.'-'.$sp_num; ?></legend>
	<table width="100%" border="1">
  <caption>
    
  </caption>
  <tr>
    <th width="10%" scope="col">Время</th>
    <th width="45%" scope="col">Четная неделя</th>
    <th width="45%" scope="col">Не четная неделя</th>
  </tr>
  <?php
  for ($i=1;$i<=6;$i++) { 
  $rasp1res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='$weekday' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='$weekday'") or die(mysql_error()); //извлекаем из базы все расписание
$rasp1 = mysql_fetch_array($rasp1res); 
$rasp0res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='$weekday' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='$weekday'") or die(mysql_error()); //извлекаем из базы все расписание
$rasp0 = mysql_fetch_array($rasp0res);
$parres = mysql_query("SELECT * FROM par_time WHERE num='$i'") or die(mysql_error()); //извлекаем из базы все расписание
$par = mysql_fetch_array($parres);
  ?>
  <tr>
    <td><?php echo $par['start'].'-'.$par['end']; ?></td>
    
    <td><input name="predmet1-<?php echo $i ?>" type="text" value="<?php if(isset($rasp1['predmet'])) {echo $rasp1['predmet'];} ?>" /> 
    <br /> В аудитории: <br /> <input name="predmet1-<?php echo $i ?>-aud" type="text" value="<?php echo $rasp1['aud']; ?>" />
    <br /> <input name="allweek-1-<?php echo $i ?>" type="checkbox" value="2" id="allweek" /><label for="allweek">Изменить на все недели</label></td>
    
    <td><input name="predmet0-<?php echo $i ?>" type="text" value="<?php if(isset($rasp0['predmet'])) {echo $rasp0['predmet'];} ?>" /> 
    <br /> В аудитории: <br /> <input name="predmet0-<?php echo $i ?>-aud" type="text" value="<?php echo $rasp0['aud']; ?>" />
    <br /> <input name="allweek-0-<?php echo $i ?>" type="checkbox" value="2" id="allweek" /><label for="allweek">Изменить на все недели</label>
   </td>
  </tr>
  <?php }; ?>
</table>

<input type="hidden" name="sp" value="<?php echo $sp; ?>" />
<input type="hidden" name="sp_num" value="<?php echo $sp_num; ?>" />
<input type="hidden" name="weekday" value="<?php echo $weekday; ?>" />
<input type="hidden" name="act" value="update" />
<br />
<input type="submit" value="Update" id="update" name="submit" class="buttons update" /><br />
</fieldset>
</form>
<?php	}
elseif ($act=="list") {
	
	$rasplistres = mysql_query("SELECT DISTINCT sp,sp_num FROM rasp ") or die(mysql_error()); //извлекаем из базы все расписание
	while ($rasplist = mysql_fetch_array($rasplistres))
	{
		echo '<a href="rasp.php?act=edit&amp;sp='.$rasplist['sp'].'&amp;sp_num='.$rasplist['sp_num'].'">Редактировать расписания группы '.$rasplist['sp'].'-'.$rasplist['sp_num'].'.</a><br />';
	}
	} 
elseif (empty($sp) || empty($sp_num)) {
?>
<a href="?act=list">Список групп с расписанием</a><br />
<form action="rasp.php" method="get" name="spec">
<fieldset>
<legend>Расписание для группы</legend>
  	<dl>
		<dt class="dtreg">Специальность:</dt>
		<?php echo '<dd class="ddreg">', $errorespzap , '</dd>'; ?>
		<dd class="ddreg">
	<select size="1" name="sp"> 
  <option value="NULL">ВЫБЕРИТЕ СПЕЦИАЛЬНОСТЬ!</option>
  <option value="NULL2"></option>
  <option value="ВКС"<?php  if ($sp=='ВКС') {echo ' selected';}; ?>>Вычислительные машины, комплексы, системы и сети</option>
  <option value="РАС"<?php  if ($sp=='РАС') {echo ' selected';}; ?>>Радиоаппаратостроение</option>
  <option value="М"<?php  if ($sp=='М') {echo ' selected';}; ?>>Менеджмент</option>
  <option value="ТМС"<?php  if ($sp=='ТМС') {echo ' selected';}; ?>>Технология машиностроения</option>
  <option value="СВ"<?php  if ($sp=='СВ') {echo ' selected';}; ?>>Сварочное производство</option>
  <option value="СД"<?php  if ($sp=='СД') {echo ' selected';}; ?>>Сестринское дело</option>
  <option value="ЛД"<?php  if ($sp=='ЛД') {echo ' selected';}; ?>>Лечебное дело</option>
  <option value="БМС"<?php  if ($sp=='БМС') {echo ' selected';}; ?>>Биотехнические и медицинские аппараты и системы</option>
	</select>
		</dd>
	</dl>
	<dl>
		<dt class="dtreg">Номер&nbsp;специальности:</dt>
		<dd class="ddreg"><input type="text" name="sp_num" maxlength="3" size="2" value="<?php echo $sp_num; ?>"  onkeyup="return proverka(this);" /> например:072, 081 <?php echo $erroresp_numzap; ?></dd>
	</dl>
    <input type="submit" value="Посмотреть / Изменить / Удалить" /><br />
</fieldset>
</form>
	
<?php } else {  ?>

<a href="?act=delete&<?php echo 'sp='.$sp.'&sp_num='.$sp_num; ?>">Удалить расписание текущей группы</a>
<table width="100%" border="1">
     <caption>
<strong>    Расписание на неделю группы <big><big><?php echo $sp.'-'.$sp_num; ?></big></big>.</strong>
  </caption>
  <tr>
    <th width="10%" scope="col">Время</th>
    <th width="30%" scope="col">Понедельник <a href="rasp.php?act=edit&weekday=1&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
    <th width="30%" scope="col">Вторник <a href="rasp.php?act=edit&weekday=2&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
    <th width="30%" scope="col">Среда <a href="rasp.php?act=edit&weekday=3&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
  </tr>
   
  <?php
  for ($i=1;$i<=6;$i++) { 
$rasp11res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='1' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='1'") or die(mysql_error()); 
$rasp11 = mysql_fetch_array($rasp11res); 
$rasp10res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='1' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='1'") or die(mysql_error()); 
$rasp10 = mysql_fetch_array($rasp10res); 
$rasp21res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='2' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='2'") or die(mysql_error()); 
$rasp21 = mysql_fetch_array($rasp21res); 
$rasp20res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='2' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='2'") or die(mysql_error()); 
$rasp20 = mysql_fetch_array($rasp20res); 
$rasp31res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='3' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='3'") or die(mysql_error()); 
$rasp31 = mysql_fetch_array($rasp31res); 
$rasp30res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='3' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='3'") or die(mysql_error()); 
$rasp30 = mysql_fetch_array($rasp30res); 
$parres = mysql_query("SELECT * FROM par_time WHERE num='$i'") or die(mysql_error()); //извлекаем из базы все расписание
$par = mysql_fetch_array($parres);
  ?>
  <tr>
	<th scope="row"><?php echo $par['start'].'-'.$par['end']; ?></th>
    <td>На четной неделе:<br /><?php if(isset($rasp11['predmet'])) {echo $rasp11['predmet'].'<br /> В аудитории '.$rasp11['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp10['predmet'])) {echo $rasp10['predmet'].'<br /> В аудитории '.$rasp10['aud'];} else {echo '<br /><hr><br />';} ?></td>
    <td>На четной неделе:<br /><?php if(isset($rasp21['predmet'])) {echo $rasp21['predmet'].'<br /> В аудитории '.$rasp21['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp20['predmet'])) {echo $rasp20['predmet'].'<br /> В аудитории '.$rasp20['aud'];} else {echo '<br /><hr><br />';} ?></td>
    <td>На четной неделе:<br /><?php if(isset($rasp31['predmet'])) {echo $rasp31['predmet'].'<br /> В аудитории '.$rasp31['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp30['predmet'])) {echo $rasp30['predmet'].'<br /> В аудитории '.$rasp30['aud'];} else {echo '<br /><hr><br />';} ?></td>
  </tr>
  <tr style="background-color:#000"><td></td><td></td><td></td><td></td></tr>
  <?php }; ?>
  <tr>
    <th scope="col">Время</th>
    <th scope="col">Четверг <a href="rasp.php?act=edit&weekday=4&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
    <th scope="col">Пятница <a href="rasp.php?act=edit&weekday=5&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
    <th scope="col">Суббота <a href="rasp.php?act=edit&weekday=6&sp=<?php echo $sp; ?>&sp_num=<?php echo $sp_num; ?>"><big><b>Изменить</b></big></a></th>
  </tr>
    <?php
  for ($i=1;$i<=6;$i++) { 
$rasp41res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='4' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='4'") or die(mysql_error()); 
$rasp41 = mysql_fetch_array($rasp41res); 
$rasp40res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='4' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='4'") or die(mysql_error()); 
$rasp40 = mysql_fetch_array($rasp40res); 
$rasp51res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='5' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='5'") or die(mysql_error()); 
$rasp51 = mysql_fetch_array($rasp51res); 
$rasp50res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='5' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='5'") or die(mysql_error()); 
$rasp50 = mysql_fetch_array($rasp50res); 
$rasp61res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='6' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='1' AND weekday='6'") or die(mysql_error()); 
$rasp61 = mysql_fetch_array($rasp61res); 
$rasp60res = mysql_query("SELECT * FROM rasp WHERE
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='2' AND weekday='6' OR
  sp='$sp' AND sp_num='$sp_num' AND predmet_num='$i' AND week='0' AND weekday='6'") or die(mysql_error()); 
$rasp60 = mysql_fetch_array($rasp60res); 
$parres = mysql_query("SELECT * FROM par_time WHERE num='$i'") or die(mysql_error()); //извлекаем из базы все расписание
$par = mysql_fetch_array($parres);
  ?>
  <tr>
	<th scope="row"><?php echo $par['start'].'-'.$par['end']; ?></th>
    <td>На четной неделе:<br /><?php if(isset($rasp41['predmet'])) {echo $rasp41['predmet'].'<br /> В аудитории '.$rasp41['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp40['predmet'])) {echo $rasp40['predmet'].'<br /> В аудитории '.$rasp40['aud'];} else {echo '<br /><hr><br />';} ?></td>
    <td>На четной неделе:<br /><?php if(isset($rasp51['predmet'])) {echo $rasp51['predmet'].'<br /> В аудитории '.$rasp51['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp50['predmet'])) {echo $rasp50['predmet'].'<br /> В аудитории '.$rasp50['aud'];} else {echo '<br /><hr><br />';} ?></td>
    <td>На четной неделе:<br /><?php if(isset($rasp61['predmet'])) {echo $rasp61['predmet'].'<br /> В аудитории '.$rasp61['aud'];} else {echo '<br /><hr><br />';} ?><hr /><hr />
    На не четной неделе:<br /><?php if(isset($rasp60['predmet'])) {echo $rasp60['predmet'].'<br /> В аудитории '.$rasp60['aud'];} else {echo '<br /><hr><br />';} ?></td>
  </tr>
  <tr style="background-color:#000"><td></td><td></td><td></td><td></td></tr>
  <?php }; ?>
</table>

<?php	
}

include ('footer.php');
} else {echo 'Недостаточно прав';};

?>